/*
  The Sniffer, phone detection unit.
 Jonathan Leang 2010
 Capstone@jonathanleang.com
 
 using Real time data API, also demo data when connection is lost
 
 */
import java.util.Collections;
import saito.objloader.*;

public class Sniffer {
  // Sniffer API data
  int s_id;
  int s_date;
  String s_sdate;
  String s_hostname;
  String s_location;
  String s_suburb;
  String s_postcode;
  String s_device_type;
  String s_mac_address;

  // Geo location, lat, long
  float geoLat  = 0;
  float geoLong = 0;
  float geoAlt  = 0;

  // Mapped location
  float locationX = 0;
  float locationY = 0;
  float locationZ = 0;

  AABB cube;
  AABB snifferShape;
  AABB snifferShape2;

  SnifferInfo snifferInfo;

  int nodesAttached = 0;

  ArrayList<Model> models;
  int modelIndex = -1;
  OBJModel model = null;
  Model xModel;

  // for be a siniffer
  Sniffer(int s_id, int s_date, String s_hostname, String s_device_type, String s_mac_address) {
    this.s_id = s_id;
    this.s_date = s_date;
    this.s_hostname = s_hostname;
    this.s_device_type = s_device_type;
    this.s_mac_address = s_mac_address;
    this.snifferShape = new AABB(new Vec3D(0,0,0),new Vec3D(SNIFFER_SIZE,SNIFFER_SIZE,SNIFFER_SIZE));
    this.snifferShape2 = new AABB(new Vec3D(0,0,0),new Vec3D(SNIFFER_SIZE*2,SNIFFER_SIZE*2,SNIFFER_SIZE*2));
    // Sniffer Info Panel
    this.snifferInfo = new SnifferInfo(0,this);
  }

  // normal real con
  Sniffer(int s_id, String s_date, String s_hostname, int location, 
  int suburb, String postcode, float latitude, float longitude,
  String s_device_type, String s_mac_address) {
    this.s_id = s_id;
    this.s_sdate = s_date;
    this.s_hostname = s_hostname;
    this.s_location = Integer.toString(location);
    this.s_suburb = Integer.toString(suburb);
    this.s_postcode = postcode;
    this.geoLat = latitude;
    this.geoLong = longitude;
    this.geoAlt = random(FLOOR_NUM);
    this.s_device_type = s_device_type;
    this.s_mac_address = s_mac_address;
    this.snifferShape = new AABB(new Vec3D(0,0,0),new Vec3D(SNIFFER_SIZE,SNIFFER_SIZE,SNIFFER_SIZE));
    this.snifferShape2 = new AABB(new Vec3D(0,0,0),new Vec3D(SNIFFER_SIZE*3,SNIFFER_SIZE*3,SNIFFER_SIZE/2));
    //snifferShape =new Sphere(30,30,30);
    // Sniffer Info Panel
    this.snifferInfo = new SnifferInfo(0,this);

    this.models = xml.getSnifferModels();
    if (this.models.size() > 0) {
      for (int i=0; i < this.models.size(); i++) {
        this.xModel = this.models.get(i);
        if (xModel.name().matches(this.s_hostname)) {
          model = new OBJModel(ptvPApplet, xModel.src(), "relative", QUADS);
          model.scale(50);
        }
      }
    }
  }

  void draw() {
    frameRate(30);
    fill(220);
    pushMatrix();
    translate(this.locationX,this.locationY,this.locationZ+SNIFFER_SIZE/2);
    pushMatrix();
    fill(color(abs(this.locationX),abs(this.locationY),20,200));
    // get sniffer models 

    if(model != null) {
      rotateX(radians(-90));
      model.draw();
      //gfx.box(this.snifferShape2);
    }
    else {
      rotateZ(radians(tick));
      gfx.box(this.snifferShape);
    }
    popMatrix();
    pushMatrix();
    translate(70,70,0);
    rotateZ(radians(tick));
    popMatrix();
    // Draw Sniffer Info Panel
    snifferInfo.draw();
    popMatrix();
  }

  float getLocationX() {
    return this.locationX;
  }

  float getLocationY() {
    return this.locationY;
  }
  float getLocationZ() {
    return this.locationZ;
  }

  void setLocationX(float var) {
    this.locationX = var;
  }

  void setLocationY(float var) {
    this.locationY = var;
  }
  void setLocationZ(float var) {
    this.locationZ = var;
  }

  String getHostname() {
    return this.s_hostname;
  }

  int getId() {
    return this.s_id;
  }
  float getGeoLat() {
    return this.geoLat;
  }
  float getGeoLong() {
    return this.geoLong;
  }
  float getGeoAlt() {
    return this.geoAlt;
  }
  void setGeoLat(float var) {
    this.geoLat = var;
  }
  void setGeoLong(float var) {
    this.geoLong = var;
  }
  void setGeoAlt(float var) {
    this.geoAlt = var;
  }
  void nodeInc() {
    nodesAttached += 1;
  }
  void nodeDec() {
    if (this.nodesAttached > 0 )
    {
      this.nodesAttached -= 1;
    }
  }
  void nodeClear() {
    this.nodesAttached = 0;
  }
  int getNodesAttached() {
    return this.nodesAttached;
  }
}
